package org.jeecgframework.web.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.CbsInvoiceInfoServiceI;
import org.jeecgframework.web.system.pojo.base.CbsInvoiceInfoEntity;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("cbsInvoiceInfoService")
@Transactional
public class CbsInvoiceInfoServiceImpl extends CommonServiceImpl implements CbsInvoiceInfoServiceI {

    private static final Logger logger = Logger.getLogger(CbsInvoiceInfoServiceImpl.class);


    public <T> void delete(T entity) {
        super.delete(entity);
        //执行删除操作配置的sql增强
        this.doDelSql((CbsInvoiceInfoEntity) entity);
    }

    public <T> Serializable save(T entity) {
        Serializable t = super.save(entity);
        //执行新增操作配置的sql增强
        this.doAddSql((CbsInvoiceInfoEntity) entity);
        return t;
    }

    public <T> void saveOrUpdate(T entity) {
        super.saveOrUpdate(entity);
        //执行更新操作配置的sql增强
        this.doUpdateSql((CbsInvoiceInfoEntity) entity);
    }

    /**
     * 默认按钮-sql增强-新增操作
     *
     * @return
     */
    public boolean doAddSql(CbsInvoiceInfoEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-更新操作
     *
     * @return
     */
    public boolean doUpdateSql(CbsInvoiceInfoEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-删除操作
     *
     * @return
     */
    public boolean doDelSql(CbsInvoiceInfoEntity t) {
        return true;
    }

    @Override
    public List<CbsInvoiceInfoEntity> getTopInvoiceByUser(String userName, String type, int top) {
        List<CbsInvoiceInfoEntity> invoices = new ArrayList<>();
        invoices = findHql("from CbsInvoiceInfoEntity where isActive=1 and createBy='" + userName + "' " + (oConvertUtils.isNotEmpty(type) ? "and invoiceGenre='" + type + "'" : "") + " order by invoiceGenre asc");
        return invoices;
    }

    @Override
    public CbsInvoiceInfoEntity snapInvoiceInfo(CbsInvoiceInfoEntity invoice) {
        if (oConvertUtils.isNotEmpty(invoice) && oConvertUtils.isNotEmpty(invoice.getId())) {
            try {
                CbsInvoiceInfoEntity newInvoice = new CbsInvoiceInfoEntity();
                MyBeanUtils.copyBean2Bean(newInvoice, invoice);
                newInvoice.setIsActive((Integer) 0);
                newInvoice.setIsDefault((Integer) 0);
                getSession().clear();
                newInvoice.setId("");
                save(newInvoice);
                return newInvoice;
            } catch (Exception e) {
                logger.error(e);
            }
        } else {
            save(invoice);
            return invoice;
        }
        return new CbsInvoiceInfoEntity();
    }

    public List<CbsInvoiceInfoEntity> getTopInvoiceByCinema(String cinemaId, String type, int top) {
        top = top <= 0 ? 0 : top;
        List<CbsInvoiceInfoEntity> results = new ArrayList<>();
        String hql = "from CbsInvoiceInfoEntity where isActive=1 and belongCinema.id='" + cinemaId + "' and invoiceGenre='" + type + "'";
        results = findHql(hql);
        if (results.size() <= top) {
            return results;
        } else {
            return results.subList(0, top - 1);
        }
    }

    @Override
    public boolean checkCanDoAdd(CbsInvoiceInfoEntity invoice) {
        if (invoice == null) {
            return false;
        } else {
            String hql = "from CbsInvoiceInfoEntity where isActive=1 ";
            if (invoice.getBelongCinema() != null && StringUtil.isEmpty(invoice.getBelongCinema().getId())) {
                invoice.setBelongCinema(null);
            } else if (invoice.getBelongCinema() != null && StringUtil.isNotEmpty(invoice.getBelongCinema().getId())) {
                hql += "and belongCinema.id='" + invoice.getBelongCinema().getId() + "' ";
            }

            if (invoice.getBelongOrg() != null && StringUtil.isEmpty(invoice.getBelongOrg().getId())) {
                invoice.setBelongOrg(null);
            }else if (invoice.getBelongOrg() != null && StringUtil.isNotEmpty(invoice.getBelongOrg().getId())) {
                hql += "and belongOrg.id='" + invoice.getBelongOrg().getId() + "' ";
            }
//            if (StringUtil.isEmpty(invoice.getInvoiceGenre())) {
//                return false;
//            } 18/6/19 不需要判断发票类型,根据影院，组织，以及税号来判断唯一性

//            String hql = "from CbsInvoiceInfoEntity where isActive=1 and invoiceGenre=? ";
//            if (StaticValue.DIC_INVOICE_GENRE_DEDICATED.equals(invoice.getInvoiceGenre())) {
                hql += " and clientTaxnum='" + invoice.getClientTaxnum() + "'";
//            } else {
//                hql += " and invoiceTitle='" + invoice.getInvoiceTitle() + "'";
//            }
            List<CbsInvoiceInfoEntity> existsInvoices = findHql(hql);
            return existsInvoices.isEmpty();
        }
    }


    /**
     * 替换sql中的变量
     *
     * @param sql
     * @return
     */
    public String replaceVal(String sql, CbsInvoiceInfoEntity t) {
        sql = sql.replace("#{id}", String.valueOf(t.getId()));
        sql = sql.replace("#{invoice_genre}", String.valueOf(t.getInvoiceGenre()));
        sql = sql.replace("#{client_name}", String.valueOf(t.getClientName()));
        sql = sql.replace("#{genre}", String.valueOf(t.getGenre()));
        sql = sql.replace("#{belong_cinema}", String.valueOf(t.getBelongCinema()));
        sql = sql.replace("#{belong_org}", String.valueOf(t.getBelongOrg()));
        sql = sql.replace("#{client_taxnum}", String.valueOf(t.getClientTaxnum()));
        sql = sql.replace("#{client_address}", String.valueOf(t.getClientAddress()));
        sql = sql.replace("#{client_phone}", String.valueOf(t.getClientPhone()));
        sql = sql.replace("#{client_bankname}", String.valueOf(t.getClientBankname()));
        sql = sql.replace("#{client_bankaccount}", String.valueOf(t.getClientBankaccount()));
        sql = sql.replace("#{create_name}", String.valueOf(t.getCreateName()));
        sql = sql.replace("#{create_by}", String.valueOf(t.getCreateBy()));
        sql = sql.replace("#{create_date}", String.valueOf(t.getCreateDate()));
        sql = sql.replace("#{update_name}", String.valueOf(t.getUpdateName()));
        sql = sql.replace("#{update_by}", String.valueOf(t.getUpdateBy()));
        sql = sql.replace("#{update_date}", String.valueOf(t.getUpdateDate()));
        sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
        return sql;
    }
}